﻿using B.S.BasicData.Api.Read.Applications.Command;
using B.S.BasicData.Api.Read.Dto;
using B.S.BasicData.Domains;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructures;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace B.S.BasicData.Api.Read.Controllers
{
    /// <summary>
    /// 控制器--读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize]
    public class UserController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<UserController> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public UserController(IMediator mediator, ILogger<UserController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回结果</returns>
        /// 
        [HttpPost]
        [AllowAnonymous]
        public Task<ApiResult<UserDto>> Login(LoginCommand command)
        {
            try
            {
                logger.LogInformation("登录成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("登录失败"+ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 权限列表
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回结果</returns>
        /// 
        [HttpGet]
        public Task<ApiResult<List<Permissons>>> PermissonList([FromQuery] PermissonQueryCommand command)
        {
            try
            {
                logger.LogInformation("权限列表展示成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("权限列表展示失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 权限明细
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回结果</returns>
        /// 
        [HttpGet]
        public Task<ApiResult<Permissons>> PermissonDetail([FromQuery] PermissonDetailCommand command)
        {
            try
            {
                logger.LogInformation("权限明细展示成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("权限明细展示失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 角色权限列表
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回结果</returns>
        /// 
        [HttpGet]
        public Task<ApiResult<List<PermissonDto>>> RoleList([FromQuery] RoleQueryCommand command)
        {
            try
            {
                logger.LogInformation("角色权限列表展示成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("角色权限列表展示失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 角色用户列表
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回结果</returns>
        /// 
        [HttpGet]
        public Task<ApiResult<ApiPaging<UserRole>>> UserRole([FromQuery] UserRoleQueryCommand command)
        {
            try
            {
                logger.LogInformation("角色用户列表展示成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("角色用户列表展示失败" + ex.Message);
                throw;
            }
        }
    }
}
